//
//  HuangwwDrawGestureViewController.m
//  Huangww
//
//  Created by huangwenwu on 2020/1/15.
//  Copyright © 2019 HuangwwFaceAndGesture. All rights reserved.
//

#import "HuangwwDrawGestureViewController.h"
#import "HuangwwPCCircleView.h"
#import "HuangwwPCCircleViewConst.h"
#import "HuangwwPCLockLabel.h"
#import "HuangwwPCCircleInfoView.h"
#import "HuangwwPCCircle.h"
#import "HuangwwAuthID.h"
@interface HuangwwDrawGestureViewController ()<CircleViewDelegate>

/**
 *  提示Label
 */
@property (nonatomic, strong) HuangwwPCLockLabel *msgLabel;

/**
 *  解锁界面
 */
@property (nonatomic, strong) HuangwwPCCircleView *lockView;

/**
 *  infoView
 */
@property (nonatomic, strong) HuangwwPCCircleInfoView *infoView;

@end

@implementation HuangwwDrawGestureViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 进来先清空存的第一个密码
    [PCCircleViewConst saveGesture:nil Key:gestureOneSaveKey];
}

- (void)viewDidLoad {
    self.viewModel = [[FULIBaseRefreshViewModel alloc] init];
    [super viewDidLoad];
    self.touchEndEditing = NO;
    self.realNavigationBarItem.title = @"设置手势密码";
    [self.view setBackgroundColor:CircleViewBackgroundColor];
    
    // 1.界面相同部分生成器
    [self setupSameUI];
    
    // 2.界面不同部分生成器
    [self setupDifferentUI];
}

#pragma mark - 界面不同部分生成器
- (void)setupDifferentUI
{
    switch (self.type) {
        case GestureViewControllerTypeSetting:
            [self setupSubViewsSettingVc];
            break;
        case GestureViewControllerTypeLogin:
//            [self setupSubViewsLoginVc];
            break;
        default:
            break;
    }
}

#pragma mark - 界面相同部分生成器
- (void)setupSameUI
{
    // 解锁界面
    HuangwwPCCircleView *lockView = [[HuangwwPCCircleView alloc] init];
    lockView.delegate = self;    
    self.lockView = lockView;
    [self.view addSubview:lockView];
    
    HuangwwPCLockLabel *msgLabel = [[HuangwwPCLockLabel alloc] init];
    msgLabel.frame = CGRectMake(0, 0, kPHONE_WIDTH, 14);
    msgLabel.center = CGPointMake(kPHONE_WIDTH/2, CGRectGetMinY(lockView.frame) - 40);
    self.msgLabel = msgLabel;
    [self.view addSubview:msgLabel];
}

#pragma mark - 设置手势密码界面
- (void)setupSubViewsSettingVc
{
    [self.lockView setType:CircleViewTypeSetting];
    
    self.title = @"设置手势密码";
    
    [self.msgLabel showNormalMsg:gestureTextBeforeSet];
    
//    PCCircleInfoView *infoView = [[PCCircleInfoView alloc] init];
//    infoView.frame = CGRectMake(0, 0, CircleRadius * 2 * 0.6, CircleRadius * 2 * 0.6);
//    infoView.center = CGPointMake(kScreenW/2, CGRectGetMinY(self.msgLabel.frame) - CGRectGetHeight(infoView.frame)/2 - 10);
//    self.infoView = infoView;
//    [self.view addSubview:infoView];
}

#pragma mark - circleView - delegate
#pragma mark - circleView - delegate - setting
- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type connectCirclesLessThanNeedWithGesture:(NSString *)gesture
{
    NSString *gestureOne = [PCCircleViewConst getGestureWithKey:gestureOneSaveKey];
    
    // 看是否存在第一个密码
    if ([gestureOne length]) {
        [self.msgLabel showWarnMsgAndShake:gestureTextDrawAgainError];
    } else {
        NSLog(@"密码长度不合法%@", gesture);
        [self.msgLabel showWarnMsgAndShake:gestureTextConnectLess];
    }
}

- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type didCompleteSetFirstGesture:(NSString *)gesture
{
    NSLog(@"获得第一个手势密码%@", gesture);
    [self.msgLabel showWarnMsg:gestureTextDrawAgain];
    
    // infoView展示对应选中的圆
    [self infoViewSelectedSubviewsSameAsCircleView:view];
}

- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type didCompleteSetSecondGesture:(NSString *)gesture result:(BOOL)equal
{
    NSLog(@"获得第二个手势密码%@",gesture);
    
    if (equal) {
        
        NSLog(@"两次手势匹配！可以进行本地化保存了");
        @weakify(self);
//        [[HuangwwLoginViewModel gesturePassword:gesture.base64String gestureSwitch:1 isDisplayTrajectory:[kGetUserDefaults(kShowLine) intValue]] subscribeNext:^(id  _Nullable x) {
//            if ([x boolValue]) {
//                @strongify(self);
//                [self.msgLabel showWarnMsg:gestureTextSetSuccess];
//                [HuangwwAuthID SaveGesturePassword:gesture];
//                [HuangwwAuthID GestureOpen];
//                if (self.delegate && [self.delegate respondsToSelector:@selector(successSaveGesture)]) {
//                    [self.delegate successSaveGesture];
//                }
//                for(UIViewController *temp in self.navigationController.viewControllers) {
//                    if([temp isKindOfClass:[HuangwwGestureUnlockViewController class]]){
//                        [self.navigationController popToViewController:temp animated:YES];
//                    }
//                }
//            }
//        }];
        
        [self.msgLabel showWarnMsg:gestureTextSetSuccess];
        [HuangwwAuthID SaveGesturePassword:gesture];
        [HuangwwAuthID GestureOpen];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        NSLog(@"两次手势不匹配！");
        [self.msgLabel showWarnMsgAndShake:gestureTextDrawAgainError];
    }
}

#pragma mark - circleView - delegate - login or verify gesture
- (void)circleView:(HuangwwPCCircleView *)view type:(CircleViewType)type didCompleteLoginGesture:(NSString *)gesture result:(BOOL)equal
{
    // 此时的type有两种情况 Login or verify
    if (type == CircleViewTypeLogin) {
        
        if (equal) {
            NSLog(@"登陆成功！");
            [self.navigationController popToRootViewControllerAnimated:YES];
        } else {
            NSLog(@"密码错误！");
            [self.msgLabel showWarnMsgAndShake:gestureTextGestureVerifyError];
        }
    } else if (type == CircleViewTypeVerify) {
        
        if (equal) {
            NSLog(@"验证成功，跳转到设置手势界面");
            
        } else {
            NSLog(@"原手势密码输入错误！");
            
        }
    }
}

#pragma mark - infoView展示方法
#pragma mark - 让infoView对应按钮选中
- (void)infoViewSelectedSubviewsSameAsCircleView:(HuangwwPCCircleView *)circleView {
    for (HuangwwPCCircle *circle in circleView.subviews) {
        
        if (circle.state == CircleStateSelected || circle.state == CircleStateLastOneSelected) {
            
            for (HuangwwPCCircle *infoCircle in self.infoView.subviews) {
                if (infoCircle.tag == circle.tag) {
                    [infoCircle setState:CircleStateSelected];
                }
            }
        }
    }
}

#pragma mark - 让infoView对应按钮取消选中

- (void)infoViewDeselectedSubviews {
    [self.infoView.subviews enumerateObjectsUsingBlock:^(HuangwwPCCircle *obj, NSUInteger idx, BOOL *stop) {
        [obj setState:CircleStateNormal];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
